package com.fattyCorps.battle.service.battle.effect;

import cn.hutool.extra.spring.SpringUtil;
import com.fattyCorps.battle.service.battle.Battle;
import com.fattyCorps.battle.service.battle.behavior.Behavior;
import com.fattyCorps.battle.service.battle.behavior.BhvProcessor;
import com.fattyCorps.battle.service.battle.data.BattleHero;
import com.fattyCorps.common.enums.battle.triggerCondition.EBehaviorCondition;
import com.fattyCorps.common.mgr.Cfg;
import com.fattyCorps.common.obj.config.BuffTypeMould;
import com.fattyCorps.common.obj.config.SkillBehaviorMould;
import com.fattyCorps.common.obj.config.SkillBuffMould;
import com.fattyCorps.common.util.GameCfgUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

// buff
@Slf4j
public class Buff {
    private Battle battle;
    @Getter
    private SkillBuffMould mould;
    @Getter
    private BuffTypeMould buffTypeMould;
    @Setter
    @Getter
    private BattleHero owner;
    private BattleHero caster;
    @Setter
    private EBehaviorCondition cnd;

    // 持续回合数量
    @Getter
    private int duration;
    // 已触发次数
    private int triggerNum;

    @Getter
    private List<Behavior> behaviors = new ArrayList<>();

    Cfg cfg;

    public Buff(Battle battle, SkillBuffMould mould, int duration, BattleHero owner, BattleHero caster, EBehaviorCondition cnd) {
        this.battle = battle;
        this.mould = mould;
        this.duration = duration;
        this.triggerNum = 0;
        this.owner = owner;
        this.caster = caster;
        this.cnd = mould.getTrgCnd();

        cfg = SpringUtil.getBean(Cfg.class);

        Integer buffTypeId = this.mould.getBuffTypeId();
        buffTypeMould = cfg.getCfg(BuffTypeMould.class, buffTypeId);

        // 如果有自定义触发时机，使用自定义触发时机
        if (mould.getTrgCnd() == null || mould.getTrgCnd().equals(EBehaviorCondition.Triggered)) {
            this.cnd = transTrgCnd(cnd);
        }

        List<Integer> bhvIds = GameCfgUtils.splitIdListStr(mould.getBehaviorIds());
        for (Integer bhvId : bhvIds) {
            SkillBehaviorMould bhvMould = cfg.getCfg(SkillBehaviorMould.class, bhvId);
            Behavior bhv = new Behavior(battle, bhvMould, owner, true);
            bhv.setCreator(caster);
            bhv.init();
            behaviors.add(bhv);
        }
    }

    // 转换触发时机，普通/技能攻击时，不能等施法者下次相关时机触发，而是要等到出手/不出手后就触发，防止施法者无法攻击的情况出现
    private EBehaviorCondition transTrgCnd(EBehaviorCondition orgCnd) {
        switch (orgCnd) {
            case SelfNorAtt:
            case SelfSkillAtt:
            case AfterSelfNorAtt:
            case AfterSelfSkillAtt:
                // 出手后就会立刻执行一次AfterSelfActive，所以要加1
                // todo:  这好像实现了伤害类buff立刻触发一次的功能。。。
                duration++;
                return EBehaviorCondition.AfterSelfActive;
            case BeforeSelfNorAtt:
            case BeforeSelfSkillAtt:
                duration++;
                return EBehaviorCondition.BeforeSelfActive;
        }

        return orgCnd;
    }

    private void trigger() {
        triggerNum ++;
        duration --;
    }

    public void update(BhvProcessor.Event e) {
        // 默认情况下，什么时候增加的buff，每回合什么时候就触发
        if (e.getCnd().equals(cnd) && (e.getTrigger() == null || e.getTrigger().equals(caster))) {
            trigger();
//            log.info("buff更新：" + this.toString());
        }
    }

    public boolean isControl() {
        return buffTypeMould.getControl();
    }

    public boolean canNorAttack() {
        return buffTypeMould.getNorAttack();
    }

    public boolean canCastSkill() {
        return buffTypeMould.getCastSkill();
    }

    public String toString() {
        String triggerName = caster == null ? "无触发者 " : "触发者：" + caster.teamDirection() + " 站位：" + caster.getStandPosNo();
        return  triggerName +
                " 拥有者：" + owner.teamDirection() + " 站位：" + owner.getStandPosNo() +
        " buff:" + mould.getName() + " 持续回合：" + duration + " 下次触发时机：" + cnd.toString();
    }
}
